package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.innovation.chengzhangriji.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TimelineVideoView extends FrameLayout implements View.OnClickListener {

    public interface OnClickVideoListener {
        void onClickVideo();
    }

    private DisplayImageOptions options;

    private ImageView mImageScreenshot;

    private CuteImageView mImagePlay;

    private ProgressBar mProgress;

    private OnClickVideoListener mOnClickVideoListener;

    public TimelineVideoView(Context context) {
        super(context);
        init(context);
    }

    public TimelineVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TimelineVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickVideoListener == null) {
            return;
        }
        if (v.getId() == R.id.btn_play) {
            mOnClickVideoListener.onClickVideo();
        }
    }

    public void setOnClickVideoListener(OnClickVideoListener onClickVideoListener) {
        mOnClickVideoListener = onClickVideoListener;
    }

    public void setScreenshotUrl(String url) {
        ImageLoader.getInstance().displayImage("file://" + url, mImageScreenshot, options, null);
    }

    public void setProgress(int progress) {
        mProgress.setProgress(progress);
    }

    public void setProgressShow(boolean show) {
        if (show) {
            mProgress.setVisibility(View.VISIBLE);
            mImagePlay.setVisibility(View.INVISIBLE);
        } else {
            mProgress.setVisibility(View.INVISIBLE);
            mImagePlay.setVisibility(View.VISIBLE);
        }
    }

    private void init(Context context) {
        setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_card));
        float density = getResources().getDisplayMetrics().density;
        int padL = (int) (16 * density);
        int padR = (int) (14 * density);
        int padY = (int) (12 * density);
        setPadding(padL, padY, padR, padY);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_timeline_video, this);
        mImageScreenshot = (ImageView) findViewById(R.id.image_screenshot);
        mImagePlay = (CuteImageView) findViewById(R.id.btn_play);
        mImagePlay.setOnClickListener(this);
        mProgress = (ProgressBar) findViewById(R.id.progress_bar);
        setOptions();
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_photo_empty)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_photo_empty)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_photo_empty)  //image加载失败
                        // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                        // .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
